package com.nhjf.admin.controller.userinformation;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.RespectBinding;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.admin.common.util.SysProperties;
import com.nhjf.admin.controller.BaseController;
import com.nhjf.admin.util.Jurisdiction;
import com.nhjf.common.page.Pagination;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.ReviewPainting;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.reviewPainting.reviewPaintingResultDto;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.ReviewPaintingQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.student.StudentService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.ReviewPaintingService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

@RequestMapping("/Uinformation")
@Controller
public class UserinformationController extends BaseController{

	@Autowired
	private StudentService studentService;
	@Autowired
	private TeachersService teachersService;
	@Autowired
	private ReviewPaintingService reviewPaintingService;
	@Autowired
	private UserService userService;
	//我的改画
	@RequestMapping("/tz_pingshen")
	public String pingshen(HttpServletRequest request, HttpServletResponse response, Model model,@RequestParam(value = "page", defaultValue = "1")Integer page,String nickname,String statis) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		reviewPaintingResultDto reviewPaintingResultDto=new reviewPaintingResultDto();
		User user=(User)Jurisdiction.getSession().getAttribute(com.nhjf.common.Constants.USER_INFO);
		try {
			System.out.println(user.getUserName());
			//老师素描/速写.../
			TeachersQo teachersQo=new TeachersQo();
			teachersQo.setUserId(user.getId());
			Teacher teacher= teachersService.queryUnique(teachersQo);
			SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(teacher!=null) {
				ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
				if(StringUtils.isNotBlank(nickname)) {
					reviewPaintingQo.setIntroduce(nickname);
					model.addAttribute("paintingQo",reviewPaintingQo);
				}
				reviewPaintingQo.setSeachTeacher(true);
				reviewPaintingQo.setTeacherId(teacher.getId());
				reviewPaintingQo.setSeachUser(true);
				if(StringUtils.isNotBlank(statis)) {
					reviewPaintingQo.setStatus(Integer.parseInt(statis));
				}
//				reviewPaintingQo.setTypeOne(2L);
				model.addAttribute("CoursesQo",reviewPaintingQo);
				Pagination pagination = new Pagination();
				pagination.setPageSize(6);
				pagination.setPageNo(page);
				pagination.setCondition(reviewPaintingQo);
				pagination = reviewPaintingService.queryPagination(pagination);
				List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
				SysProperties sysProperties=SysProperties.getInstance();
				String imagePath=sysProperties.get("webPath");
				for(ReviewPainting reviewPainting:reviewPaintingList) {
					reviewPainting.setReleasedate(format1.format(reviewPainting.getReleasetime()));
					if(StringUtils.isNotEmpty(reviewPainting.getChangepaintingURL())){
						reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
					}
					StudentQo qo=new StudentQo();
					qo.setSeachStudent(true);
					qo.setUserid(reviewPainting.getUser().getId());
					Student student= studentService.queryUnique(qo);
					reviewPainting.setNickName(student.getNickname());
					if(!StringUtils.isEmpty(student.getAvatar())) {
						if(student.getAvatar().indexOf(imagePath+"/picture/")>=0) {

						}else {
							student.setAvatar(imagePath+"/picture/"+student.getAvatar());
							reviewPainting.setAvatar(student.getAvatar());
						}
					}
					if(StringUtils.isNotEmpty(reviewPainting.getNotchangedURL())){
						reviewPainting.setNotchangedURL(imagePath+"/reviewPainting/"+reviewPainting.getNotchangedURL());
					}
					UserQo userQo1=new UserQo();
					userQo1.setUserId(reviewPainting.getUser().getId());
					User user1=userService.queryUnique(userQo1);
					reviewPainting.getUser().setId(user1.getId());
				}
				
				reviewPaintingResultDto.setTotalPage(pagination.getTotalPage());
				reviewPaintingResultDto.setReviewPaintinglist(reviewPaintingList);
				reviewPaintingResultDto.setIsRole(2);
				SysProperties sysProperties1=SysProperties.getInstance();
				String webPath=sysProperties1.get("webPath");
				model.addAttribute("pagination",pagination);
				model.addAttribute("reviewPaintingResultDto",reviewPaintingResultDto);
				model.addAttribute("webPath",webPath);
				return "/review/teacher.html";
			}else {
				return "/error/error.html";
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出！");
			return JSONObject.fromObject(resMap).toString();
		}
	}
	@RequestMapping("/reviewDetails")
	public String reviewDetails(Model model,String id) {
		ReviewPaintingQo paintingQo=new ReviewPaintingQo();
		paintingQo.setReviewId(Long.parseLong(id));
		paintingQo.setSeachUser(true);
		paintingQo.setSeachTeacher(true);
		ReviewPainting painting= reviewPaintingService.queryUnique(paintingQo);
		StudentQo qo=new StudentQo();
		qo.setSeachStudent(true);
		qo.setUserid(painting.getUser().getId());
		SysProperties sysProperties=SysProperties.getInstance();
		String imagePath=sysProperties.get("webPath");
		Student student= studentService.queryUnique(qo);
		student.setAvatar(imagePath+"/picture/"+student.getAvatar());
		painting.setAvatar(student.getAvatar());
		painting.setNickName(student.getNickname());
		model.addAttribute("webPath",imagePath);
		model.addAttribute("painting",painting);
		return "/review/tan.html";
	}
	
}
